import { Schema, model } from "mongoose";
import type Typings from "../../typings";

import { createDb } from "../../mongoose/mongoose";
const db = createDb("todo", "develop");

// 定义 Schema 模型
const DevelopSchema = new Schema<Typings.Develop.props>({
  id: {
    type: String,
    required: true,
    unique: true,
  },

  title: {
    type: String,
    required: true,
  },

  content: {
    type: String,
    required: true,
  },

  isFinish: {
    type: Boolean,
    required: true,
  },

  isTop: {
    type: Boolean,
    required: true,
  },

  createdTime: {
    type: Number,
    Number,
    required: true,
    default: Date.now(),
  },

  updatedTime: {
    type: Number,
    required: true,
    default: Date.now(),
  },

  creatorId: {
    type: String,
    required: true,
    default: "",
  },
});

// 排除 _id 和 __v 字段
DevelopSchema.set("toObject", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret: any) => {
    delete ret._id;
    delete ret.__v;
  },
});

DevelopSchema.set("toJSON", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret: any) => {
    delete ret._id;
    delete ret.__v;
  },
});

const DevelopModel = db.model<Typings.Develop.props>(
  "Develop",
  DevelopSchema,
  "develops"
);

export default DevelopModel;
